/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cl.darksigns.webstat.front.actions;

import cl.darksigns.webstat.front.actionforms.LoginActionForm;
import cl.darksigns.webstat.front.actions.delegate.BusinessDelegate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author _ViKtOr
 */
public class LoginAction extends org.apache.struts.action.Action {
    
    Logger logger = Logger.getLogger(LoginAction.class);
    /* forward name="success" path="" */
   
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        LoginActionForm loginForm = (LoginActionForm)form;
        String accion = loginForm.getAccion()!=null?loginForm.getAccion():"loginPage";
        BusinessDelegate delegate = new BusinessDelegate();
        
        
        String destino = "";
        
        if("validarUsuario".equalsIgnoreCase(accion)){
            logger.info("Validating user: " + loginForm.getUserName());
            boolean ret = delegate.validateUser(loginForm.getUserName(), loginForm.getUserPwd());
            if(ret == true){
                logger.info("The User is valid");
                destino = "mainPage";
            }else{
                logger.info("The user is invalid");
                destino = "loginPage";
            }
        }else{
            destino = "loginPage";
        }
        
        return mapping.findForward(destino);
        
    }
}